package shortcircuit.graphics;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import shortcircuit.Display;

/**
 * Creates the level selection list for the initial screen of Puzzle Mode.  Contains
 * all of the playable levels offered in Puzzle Mode in either a locked or an 
 * unlocked status.  A specified numbers is allowed to be viewed at a time, and the
 * user can scroll between levels by clicking on the left or right arrow buttons.
 * Levels are JButtons with images, and the clicking of one of these levels
 * will trigger PuzzleEngine to run the specified level by loading the appropriate
 * map.
 * 
 * 
 * @author Kevin Lewi
 *
 */
public class ListPanel extends JPanel
{
	private JButton[] _Levels;
	private JButton leftArrowButton, rightArrowButton;
	private JPanel scrollingPane;
	private Dimension _Dimension;
	private int selectedLevel, startLevel;
	
	/**
	 * Creates a new ListPanel, allowing for customization of panel dimensions
	 * and panel length in terms of levels displayed at one time.
	 * 
	 * @param panelWidth Width of the panel
	 * @param panelHeight Height of the panel
	 * @param listSize Number of levels displayed at one time
	 * @author Kevin Lewi
	 */
	public ListPanel(int panelWidth, int panelHeight, ArrayList<Integer> levelStatuses)
	{
		setOpaque(false);
		setLayout(null);
		setSize(panelWidth, panelHeight);
		setLocation(0, 0);
		_Dimension = new Dimension(panelWidth, panelHeight);
		_Levels = new JButton[levelStatuses.size()];
		for(int i = 0; i < 13; i++)
		{
			int levelStatus = levelStatuses.get(i);
			JButton tempButton = new JButton();
			if(levelStatus == 0)
				tempButton.setEnabled(false);
			tempButton.setSize(40, 40);
			tempButton.setLocation(i % 8 * 50, i / 8 * 50);
			tempButton.setBorder(null);
			tempButton.setContentAreaFilled(false);
			if(levelStatus == 0 || levelStatus == 1)
			tempButton.setIcon(new ImageIcon(new ImageIcon("resources/Level.jpg").getImage().getScaledInstance(40, 40, Image.SCALE_SMOOTH)));
			if(levelStatus == 2)
				tempButton.setIcon(new ImageIcon(new ImageIcon("resources/Level_Completed.jpg").getImage().getScaledInstance(40, 40, Image.SCALE_SMOOTH)));
			tempButton.setDisabledIcon(new ImageIcon(new ImageIcon("resources/Level_Locked.jpg").getImage().getScaledInstance(40, 40, Image.SCALE_SMOOTH)));
			tempButton.setRolloverIcon(new ImageIcon(new ImageIcon("resources/Level_Hovered.png").getImage().getScaledInstance(40, 40, Image.SCALE_SMOOTH)));
			tempButton.setPressedIcon(new ImageIcon(new ImageIcon("resources/Level_Pushed.jpg").getImage().getScaledInstance(40, 40, Image.SCALE_SMOOTH)));
			tempButton.addActionListener(new LevelListener());
			_Levels[i] = tempButton;

		}
		
		scrollingPane = new JPanel();
		scrollingPane.setOpaque(false);
		scrollingPane.setLayout(null);
		scrollingPane.setLocation(20, 80);
		scrollingPane.setSize(900, 900);
		
		update();
	}

	
	/**
	 * Returns the level selected on the ListPanel
	 * 
	 * @return The selected level
	 * @author Kevin Lewi
	 */
	public int getSelectedLevel()
	{
		return selectedLevel;
	}
	
	/**
	 * Refreshes the panel by re-adding all of the levels to the current view
	 * 
	 * @author Kevin Lewi
	 */
	public void update()
	{
		setVisible(false);
		removeAll();
		scrollingPane.removeAll();
		for(int i = 0; i < _Levels.length; i++)
		{
			try
			{
				scrollingPane.add(_Levels[i]);
			}
			catch(NullPointerException e) {}
		}

		add(scrollingPane);
		setSize(_Dimension.width, _Dimension.height);
		setVisible(true);
	}
	
	/**
	 * Returns the selected level when a button is clicked on the level selecting
	 * screen
	 * 
	 * @author Kevin Lewi
	 *
	 */
	private class LevelListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			for(int index = 0; index < _Levels.length; index++)
			{
				if(_Levels[index] == e.getSource())
				{
					selectedLevel = index + 1;
					break;
				}
			}
			
		}
	}
	
	private class ScrollingListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == leftArrowButton)
			{
				startLevel -= 5;
			}
			if(e.getSource() == rightArrowButton)
			{
				startLevel += 5;
			}
			update();
		}
	}
}
